package com.controller;


import com.pojo.Stu;
import com.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/stu")
public class StuController {

    @Autowired
//    @Qualifier("StuServiceImpl")//自动装配把autowire = “byName”
    private StuService stuService;

    // 查询全部
    @RequestMapping("allstu")
    public String allstu(Model model) {
        List<Stu> list = stuService.queryAllstu();
        model.addAttribute("list", list);
        System.out.println("the: " + list);
        return "allstu";
    }

    // 删除
    @RequestMapping("/del/{id}")
    public String deleteid(@PathVariable("id") int id) {
        stuService.deleteByid(id);
        return "redirect:/stu/allstu";
    }

    //添加
    @RequestMapping("/toAddstu")
    public String toAddPaper() {
        return "addstu";
    }

    @RequestMapping("/addstu")
    public String addPaper(Stu stu) {
        System.out.println(stu);
        stuService.addstu(stu);
        return "redirect:/stu/allstu";
    }


    //修改
    @RequestMapping("/toUpdatestu")
    public String toUpdatestu(Model model, int id) {
        Stu stu = stuService.queryByid(id);
        System.out.println(stu);
        model.addAttribute("stu", stu);
        return "updatestu";
    }

    @RequestMapping("/updatestu")
    public String updatestu(Model model, Stu stu) {
        System.out.println(" update: " + stu);
        stuService.updatestu(stu);
        stu = stuService.queryByid(stu.getId());
        System.out.println(" update: " + stu);
        return "redirect:/stu/allstu";
    }


    // 根据姓名查询学生
    @RequestMapping("/querystu")
    public String queryByname(String name, Model model) {
        System.out.println("query Name :" + name);
        List<Stu> list = new ArrayList<>();
        list.addAll(stuService.queryByname(name));
        if (list.isEmpty()) {
            model.addAttribute("error", "not found");
        }
        model.addAttribute("list", list);
        return "allstu";
    }

    // 根据专业查询学生
    @RequestMapping("/querymajor")
    public String queryBymajor(String major, Model model) {
        System.out.println("query major :" + major);
        List<Stu> list = new ArrayList<>();
        list.addAll(stuService.queryBymajor(major));
        if (list.isEmpty()) {
            model.addAttribute("error", "not found");
        }
        model.addAttribute("list", list);
        return "allstu";
    }

    // 根据学号查询学生
    @RequestMapping("/querysid")
    public String queryBysid( int sid, Model model) {
        System.out.println("query sid :" + sid);
        List<Stu> list = new ArrayList<>();
        list.addAll(stuService.queryBysid(sid));
        if (list.isEmpty()) {
            model.addAttribute("error", "not found");
        }
        model.addAttribute("list", list);
        return "allstu";
    }


}
